//
//  IGTopicVideoCell.m
//  jingdongfang
//
//  Created by BOE on 2017/12/14.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGTopicVideoCell.h"
#import <AVFoundation/AVFoundation.h>
#import "IGTopicModel.h"
#import "LJZFPlayerView.h"
#import "ZFPlayerModel.h"
#import "LJZFPlayerControlView.h"

@interface IGTopicVideoCell ()
@property (nonatomic, strong) UIImageView *movieImgView;
@property (nonatomic, strong) IGTopicModel *feedDetail;
@property (nonatomic, strong) LJZFPlayerControlView *controlView;
@property (nonatomic, strong) ZFPlayerModel *playerModel;
@property (nonatomic, assign) BOOL flag;//控制防止tableview上下滑动多次加载视图导致重叠
@property (nonatomic, copy) NSString *movieUrl;

@end

static NSString *const kIGTopicVideoCellID = @"kIGTopicVideoCellID";

@implementation IGTopicVideoCell

+ (IGTopicVideoCell *)cellWithTableView:(UITableView *)tableView
{
    IGTopicVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:kIGTopicVideoCellID];
    if (!cell) {
        cell = [[IGTopicVideoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIGTopicVideoCellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _flag = NO;
        self.contentView.backgroundColor = [UIColor c3Color];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}

- (void)createUI
{
    [self.contentView addSubview:self.movieImgView];
}

- (UIImageView *)movieImgView
{
    if (!_movieImgView) {
        _movieImgView = [[UIImageView alloc] init];
        _movieImgView.backgroundColor = [UIColor blackColor];
        _movieImgView.userInteractionEnabled = YES;
        _movieImgView.tag = 10000;
    }
    return _movieImgView;
}

- (LJZFPlayerView *)playerView
{
    if (!_playerView) {
        _playerView = [[LJZFPlayerView alloc] init];
    }
    return _playerView;
}

- (LJZFPlayerControlView *)controlView
{
    if (!_controlView) {
        _controlView = [[LJZFPlayerControlView alloc] init];
    }
    return _controlView;
}

- (ZFPlayerModel *)playerModel
{
    if (!_playerModel) {
        _playerModel = [[ZFPlayerModel alloc] init];
        _playerModel.videoUrl = _movieUrl;
        _playerModel.tableView = _tableView;
        _playerModel.indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
        _playerModel.cellImageViewTag = _movieImgView.tag;
    }
    return _playerModel;
}

- (void)loadCellWithDetail:(IGTopicModel *)feedDetail images:(NSArray *)images
{
    if (!feedDetail || _feedDetail == feedDetail || _flag) {
        return;
    }
    
    _feedDetail = feedDetail;
    _movieUrl = _feedDetail.movieUrl;
    
    CGFloat cellHeight = [IGTopicVideoCell cellHeight:feedDetail];
    [_movieImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(cellHeight - kGAP15);
    }];
    
//    LJFeedDetailImages *detailImage = nil;
//    if (images.count > 1) {
//        detailImage = images[1]; // 视频帧截图
//    } else if (images.count > 0) {
//        detailImage = images[0]; // 上传图
//    }
    
//    //注意：替换 | 为 %7C 不然[NSURL URLWithString:]解析不了
//    NSString *urlStr = [detailImage.imageUrl stringByReplacingOccurrencesOfString:@"|" withString:@"%7C" options:NSBackwardsSearch range:NSMakeRange(0, detailImage.imageUrl.length)];
    if (feedDetail.coverUrl) {
        __weak typeof(self) weakSelf = self;
        [self.movieImgView sd_setImageWithURL:[NSURL URLWithString:feedDetail.coverUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [weakSelf loadPlayerViewWithImage:image];
        }];
    } else {
        [self loadPlayerViewWithImage:nil];
    }
}

- (void)loadPlayerViewWithImage:(UIImage *)image
{
    self.playerModel.placeholderImage = image;
    self.playerView.playerModel = self.playerModel;
    [self.movieImgView addSubview:self.playerView];
    [self.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    self.controlView.videoScreenRatio = self.feedDetail.videoScreenRatio;
    self.playerView.controlView = self.controlView;
    
    [self.playerView autoPlayTheVideo];
}

+ (CGFloat)cellHeight:(IGTopicModel *)feedDetail
{
    CGFloat cellHeight = KSCREENHEIGHT;
    CGFloat cellWidth = KSCREENWIDTH;
    CGFloat height = [feedDetail.video.movieHeight floatValue];
    CGFloat width = [feedDetail.video.movieWidth floatValue];
    if (feedDetail.videoScreenRatio == VideoScreenRatioWSH) {
        cellHeight = KSCREENHEIGHT - 64 - 60 - 76;
        if (height != 0) {
            cellWidth = width * cellHeight/height;
        }
    } else if (feedDetail.videoScreenRatio == VideoScreenRatioWEH) {
        cellHeight = cellWidth;
    } else if (feedDetail.videoScreenRatio == VideoScreenRatioWBH) {
        if (width != 0) {
            cellHeight = height * cellWidth/width;
        }
    }
    
    return cellHeight + kGAP15;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
